package day6.Component;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

public class TestCanvas extends JFrame{
	private Canvas can;
	public TestCanvas(String title) {
	this.setTitle(title);
	this.setSize(400, 500);
	this.setLocationRelativeTo(null);
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	initComponent();
	
	// 放到最后显示窗口
	this.setVisible(true);
}
// 初始化
private void initComponent() {
//借助画布工具
	 can = new Canvas(){

		@Override
		public void paint(Graphics g) {
			g.setColor(Color.pink);
			g.drawLine(0, 0, can.getWidth(),can.getHeight());
		}
		
	};
	can.addMouseMotionListener(new MouseAdapter() {

		@Override
		public void mouseDragged(MouseEvent e) {
             Graphics g = can.getGraphics();
             g.setColor(Color.pink);
             //以当前坐标为实心点  拖动
             g.fillOval(e.getX(), e.getY(), 30, 30);
		}
		
	});
	this.add(can);
}
public static void main(String[] args) {
	new TestCanvas("画布");
}


}
